package com.pepper.k8sdemo1.controller;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
public class HelloContrller {

    @GetMapping("")
    public String index(){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern( "yyyy/MM/dd HH:mm:ss" );
        LocalDateTime now = LocalDateTime.now();
        String strNow = dtf.format(now);
        String resp = "k8sdemo1 index now: " + strNow ;
        System.out.println(resp);
        return resp;
    }

    @GetMapping("/test")
    public String test(){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern( "yyyy/MM/dd HH:mm:ss" );
        LocalDateTime now = LocalDateTime.now();
        String strNow = dtf.format(now);
        String resp = "k8sdemo1 test, now: " + strNow ;

        System.out.println(resp);
        return resp;
    }
    @GetMapping("/jdkInfo")
    public String getjdk(){
        String arch = System.getProperty("os.arch");
        String vmName = System.getProperty("java.vm.name");
        String jdkVersion = System.getProperty("java.version");
        return  arch + "_"+ vmName + "_" + jdkVersion;
    }
    @GetMapping("/netInfo")
    public JSONObject netInfo() throws UnknownHostException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code","0");
        jsonObject.put("message","成功");
        JSONObject data = new JSONObject();
        InetAddress localHost = InetAddress.getLocalHost();
        String address = localHost.getHostAddress();
        String hostName = localHost.getHostName();
        System.out.println("IP地址：" + localHost.getHostAddress() + "，主机名：" + localHost.getHostName());
        data.put("address", address);
        data.put("hostname", hostName);
        jsonObject.put("data",data);
        return jsonObject;
    }
}
